<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta name="keywords" content="这次封装个好玩的，获取图片试一试？">
  <meta name="description" content="api接口调用来自https://api.uomg.com/">
  <title>美女图随机获取</title>
  <style>
    #content{
      display: flex;
      justify-content: center;
      align-items: center;
    }
    #img{
      margin-top: 200px;
      width: 700px;
    }
  </style>
</head>
<body>
  <div id="content">
    <img src="https://ae01.alicdn.com/kf/HTB1tJb_Xq1s3KVjSZFt761LOpXa5.png" id="img" alt="">
  </div>
</body>
</html>

<script>
  //获取img对象
  const img = document.querySelector('#img');
  //开启ajax请求
  sendAjax('GET','https://api.uomg.com/api/rand.img1?sort=美女&format=json')
  .then(value=>{
    console.log(value);
  },err=>{
    console.log(err);
  })




  function sendAjax(method,url){
    return new Promise((resolve,reject)=>{
      const xhr = new XMLHttpRequest();
      //开启ajax请求
      xhr.open(method,url);
    //发送
      xhr.send();
      //处理响应结果
      xhr.onreadystatechange = function(data){
        if(xhr.readyState === 4){
          if(xhr.status >= 200 && xhr.status < 300){
            resolve(data.currentTarget.responseText);
          }else{
            //失败
            reject(xhr.status);
          }
        }
      }
    })
  }
</script>


